/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Copyright 2002-2003 by Mark Wolgemuth, All rights reserved.             */
/* Copyright 2002-2003 by Employease, Inc.  All rights reserved.           */
/*                                                                         */
/* Redistribution and use in source and binary forms, with or without      */
/* modification, are permitted provided that the following conditions      */
/* are met:                                                                */
/*                                                                         */
/*    1. Redistributions of source code must retain the above copyright    */
/* notice, this list of conditions and the following disclaimer.           */
/*    2. Redistributions in binary form must reproduce the above copyright */
/* notice, this list of conditions and the following disclaimer in the     */
/* documentation and/or other materials provided with the distribution.    */
/*                                                                         */
/* THIS SOFTWARE IS PROVIDED BY THE MOD_ATHENA PROJECT ``AS IS'' AND ANY   */
/* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE       */
/* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR      */
/* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR        */
/* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,   */
/* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,     */
/* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR      */
/* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY     */
/* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT            */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   */
/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef ATH_UTIL_H
#define ATH_UTIL_H

#include "apr.h"
#include "apr_strings.h"
#include "apr_pools.h"
#include "apr_file_io.h"
#include "apr_time.h"
#include "auto_config.h"

#include "ath_list.h"
#include "athd_util_types.h"

#ifdef ATH_DEBUG
#ifndef DEBUGOUT
#define        DEBUGOUT(fmt, args...) \
                  ((fprintf(stdout, "%s(%d,%s): " fmt "\n", \
                  __FILE__, __LINE__ , __FUNCTION__, ##args)) \
                  ? (fflush(stdout)) : (void)0)
#endif /* DEBUGOUT */
#ifndef DEBUGERR
#define        DEBUGERR(fmt, args...) \
                  ((fprintf(stderr, "%s(%d,%s): " fmt "\n", \
                  __FILE__, __LINE__ , __FUNCTION__, ##args)) \
                  ? (fflush(stderr)) : (void)0)
#endif /* DEBUGERR */
#else /* ATH_DEBUG */
#ifndef DEBUGOUT
#define        DEBUGOUT(fmt, args...) ((void)0)
#endif /* DEBUGOUT */
#ifndef DEBUGERR
#define        DEBUGERR(fmt, args...) ((void)0)
#endif /* DEBUGERR */
#endif /* ATH_DEBUG */

#define    ATH_NULL_STR        "NULL"

/* useful printing macros */

#define ATHD_PRINTOUT(fmt, args...) \
   ((fprintf(stdout, fmt, ##args)) ? (fflush(stdout)) : (void)0)
#define ATHD_PRINTERR(fmt, args...) \
   ((fprintf(stderr, "%s(%d,%s): " fmt "\n", \
   __FILE__, __LINE__ , __FUNCTION__, ##args)) \
   ? (fflush(stdout)) : (void)0)
/* exits after error */
#define ATHD_PRINTERR_CRIT(fmt, args...) \
   ((fprintf(stderr, "%s(%d,%s): " fmt "\n", \
   __FILE__, __LINE__ , __FUNCTION__, ##args)) \
   ? (athd_proc_exit(1)) : (void)0)

#define XTH_SEC(x) ((x > 0) ? ((apr_time_from_sec(1)) / x) : (0))

#ifndef CTRL
#define CTRL(c) ((c) & 0x1f)
#endif

/* pthread_pool_cleanup */
#ifdef HAVE_PTHREAD_H
apr_status_t   pthread_mutex_cleanup(void *mtx);
apr_status_t   pthread_cond_cleanup(void *cond);
#endif /* HAVE_PTHREAD_H */

/* ath_iterator.c */
ath_iterator * ath_iterator_create(apr_pool_t *pool, ath_list *list);
int            ath_iterator_hasnext(ath_iterator *iter);
void *         ath_iterator_getnext(ath_iterator *iter);

/* util/parse.c */
int            ath_skip_ws(char *str, int limit);
#define        ATHD_MAX_GETLINE 4096
char *         ath_getline(apr_pool_t *pool, apr_file_t *file, int limit);
char *         ath_chomp(char *str);
char *         ath_chompn(char *str, int limit);
char *         ath_chomp_ws(char *str);
char *         ath_trim_comment(char *str);
int            ath_flag_is_on(const char *str_test);
int            ath_streq(const char *str1, const char *str2);
char *         ath_list_str_join(apr_pool_t *pool, 
				 const char *sep, ath_list *list);
int            ath_str_is_error(const char *str);
int            ath_str_is_warn(const char *str);
void           ath_str_to_apr_time(char *interval, apr_time_t *time);
#define ATHD_SIGSTRT           -2
int            ath_str_to_unix_sig(const char *strsig);
char *         ath_ap_getword_conf(apr_pool_t *pool, const char **str);

/* util/directive.c */
#define        ATHD_ERR_TAGFMT "ERROR: Tag missing closing bracket: %s"
#define        ATHD_ERR_TAGOPN "ERROR: Tag of different type still open: %s"
athd_directive * athd_directive_alloc(apr_pool_t *p);
athd_directive * athd_directive_new(apr_pool_t *p, char *cmd, char *arg);
athd_directive * athd_directive_add_child(apr_pool_t *p, 
					  athd_directive *d_parent, 
					  char *cmd, char *arg, int line);
void             athd_directive_add_arg(apr_pool_t *p, athd_directive *d, 
					char *arg);
const char *     athd_directive_gen_tree(apr_pool_t *p, 
		    athd_directive **dtree, const char *path);
int              athd_directive_traverse(athd_directive **dstate, 
					  athd_tree_state *tstate);
char *           athd_directive_tree_to_str(apr_pool_t *pool,
					    athd_directive *dtree);
athd_directive * athd_directive_for_cmd(athd_directive *dtree, char *cmd);

/* util/athd_error.c */
#define        ATHD_MAX_STRERR 1024  /* limit to err msg size */
char *           athd_err_str(apr_pool_t *p, apr_status_t rv);

#endif /* ATH_UTIL_H */
